package cn.jiguang.demo.test;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

//参考资料：
//https://so.csdn.net/so/search?spm=1001.2101.3001.7499&q=Android%20%E5%BA%94%E7%94%A8%E6%A1%8C%E9%9D%A2%E8%A7%92%E6%A0%87&t=&u=&utm_medium=distribute.pc_search_hot_word.none-task-hot_word-alirecmd-3-Android%20%E5%BA%94%E7%94%A8%E6%A1%8C%E9%9D%A2%E8%A7%92%E6%A0%87-null-null.172%5Ev8%5Econtrol&depth_1-utm_source=distribute.pc_search_hot_word.none-task-hot_word-alirecmd-3-Android%20%E5%BA%94%E7%94%A8%E6%A1%8C%E9%9D%A2%E8%A7%92%E6%A0%87-null-null.172%5Ev8%5Econtrol
//https://www.jianshu.com/p/b09c0a1fb540
//https://github.com/nuonuoOkami/DesktopCornerMark

//https://blog.csdn.net/jxj960417/article/details/135496171

//https://blog.csdn.net/qq_39389024/article/details/112848918

//https://blog.csdn.net/znouy/article/details/123587737
public class BadgeManager {

    public static void setVivoBadgeNumber(Context context, int number) {
        try {
            Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
            intent.putExtra("packageName", context.getPackageName());
            String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
            intent.putExtra("className", launchClassName);
            intent.putExtra("notificationNum", number);
            context.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void setHuaWeiBadgeNumber(Context context, int number) {
        try {
            if (number < 0) number = 0;
            Bundle bundle = new Bundle();
            bundle.putString("package", context.getPackageName());
            String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
            bundle.putString("class", launchClassName);
            bundle.putInt("badgenumber", number);
            context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void setOppoBadgeNumber(Context context, int number) {
        try {
            if (number <= 0) {
                number = -1;
            }
            Intent intent = new Intent("com.oppo.unsettledevent");
            intent.putExtra("pakeageName", context.getPackageName());
            intent.putExtra("number", number);
            intent.putExtra("upgradeNumber", number);
            if (canResolveBroadcast(context, intent)) {
                context.sendBroadcast(intent);
            } else {
                try {
                    Bundle extras = new Bundle();
                    extras.putInt("app_badge_count", number);
                    context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extras);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean canResolveBroadcast(Context context, Intent intent) {
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
        return receivers != null && receivers.size() > 0;
    }


    //在调用NotificationManager.notify(notifyID, notification)这个方法之前先设置角标显示的数目

    public static void setXiaoMiBadgeNumber(Notification notification, int number) {
        try {
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
            method.invoke(extraNotification, number);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
